package com.adingxiong.cft.controller;


import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.adingxiong.cft.common.entity.DDuser;
import com.adingxiong.cft.config.MinioProperties;
import com.adingxiong.cft.config.component.MinioTemplate;
import com.adingxiong.cft.result.Result;
import com.adingxiong.cft.util.HttpUtils;
import com.adingxiong.cft.util.ImgUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @ClassName MiniOSSController
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/1/21 13:33
 **/
@RestController
@RequestMapping("/mini")
@Api(tags = "mini oss 文件服务")
public class MiniOSSController {

    @Autowired
    private MinioTemplate template;

    @Autowired
    private MinioProperties properties;

    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");

    @PostMapping(value = "/upload")
    @ApiOperation(value = "文件上传")
    public Result upload(
            @ApiParam(value = "文件",required = true) @RequestParam(value = "file")MultipartFile file,
            @ApiParam(value = "文件通道名称",required = true) @RequestParam(value = "bucketName",defaultValue = "file") String bucketName,
            @ApiParam(value = "链接过期时间") @RequestParam(value = "expire",required = false,defaultValue = "7") Integer expire,
            @ApiParam(value = "是否创建时间文件夹") @RequestParam(value = "isCreateFile",defaultValue = "false") boolean isCreateFile
            ) {
        String netUrl = null;
        String fileOrginName = file.getOriginalFilename();
        ImgUtil.checkFile(file,properties);
        StringBuilder timefile = new StringBuilder();
        Map<String,String> res = new HashMap<>();
        if(isCreateFile){
            //创建时间文件夹
            timefile.append(sdf.format(new Date()));
            timefile.append("/");
        }
        String newFileName = UUID.randomUUID().toString().replaceAll("-","");
        timefile.append(newFileName);
        timefile.append(fileOrginName.substring(fileOrginName.lastIndexOf(".")));
        template.createBucket(bucketName);
        try {
            template.putObject(bucketName,timefile.toString(),file.getInputStream(),file.getContentType());
            //netUrl =
            //返回nginx代理之后的静态资源路径
            netUrl = getNetUrl(bucketName,timefile.toString(),properties);
            res.put("lastUrl",netUrl);
            res.put("interimUrl" ,template.getNetUrl(bucketName, timefile.toString(), expire*1000*60));

        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result.success(res);
    }

    public String getNetUrl(String BucketName,String fileName,MinioProperties properties) {
        return properties.getHttpUrl() + BucketName + "/" + fileName;
    }




    @GetMapping("/dd/list")
    public Result<List<DDuser>> getDDUserList() {

      List<DDuser> list =  HttpUtils.getDDList();
      return Result.success(list);

    }
}
